
#include <stdio.h>
#include <string.h>

struct opcode
{
    char* rtype;
    char* type1;
    char* type2;
    char* type3;
    int mem_size;
    int prefix;
    int code;
    char* name;
    char* text;
};

struct opcode ops[] = {
#define WABT_OPCODE(rtype, type1, type2, type3, mem_size, prefix, code, Name, \
                    text)                                                     \
  { #rtype, #type1, #type2, #type3, mem_size, prefix, code, #Name, text },
#include "opcode.def"
#undef WABT_OPCODE
};

void print_type(char* name, char* s)
{
    printf("            %s = ", name);
    if (0 == strcmp(s, "___"))
    {
        printf("None;\n");
    }
    else
    {
        printf("Some %s;\n", s);
    }
}

int main(int argc, char** argv)
{
    printf("// this file is automatically generated\n");
    printf("module fsgen.opcodes\n");
    printf("    open fsgen.types\n");
    printf("\n");

    int num = sizeof(ops) / sizeof(struct opcode);

    printf("    let opcode_infos = [\n");
    for (int i=0; i<num; i++)
    {
        printf("        {\n");
        print_type("rtype", ops[i].rtype);
        print_type("type1", ops[i].type1);
        print_type("type2", ops[i].type2);
        print_type("type3", ops[i].type3);
        printf("            mem_size = %d;\n", ops[i].mem_size);
        if (ops[i].prefix == 0)
        {
            printf("            prefix = None;\n");
        }
        else
        {
            printf("            prefix = Some 0x%02x;\n", ops[i].prefix);
        }
        printf("            code = 0x%02x;\n", ops[i].code);
        printf("            name = \"%s\";\n", ops[i].name);
        printf("            text = \"%s\";\n", ops[i].text);
        printf("        }\n");
    }
    printf("    ]\n");

    return 0;
}

